<?php
if (!defined('BASEPATH'))
	exit ('No direct script access allowed');
/*
 * Created on Jul 7, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */

/*
 *
 *
 */
function public_base_url() {
	return base_url() . 'public/';
}

/**
 *
 *
 */
function image_url() {
	return public_base_url() . 'images/';
}
/** general**/
/**
 * Show var dump. pre_var_dump() is used for testing only!
 *
 * @param mixed $var
 * @return null
 */
function pre_var_dump($var) {
	print "<pre style=\"text-align: left\">\n";
	var_dump($var);
	print "</pre>\n";
} // pre_var_dump

/** end general**/

/**
 *
 *
 */
function userfile_base_url($var) {
	return base_url() . 'userfiles/' . $var;
}
/**
 *
 *
 */
function userfile_img_url($var) {
	$var = str_replace('userfiles/image/','',$var);
	return base_url() . 'userfiles/image/' . $var;
}

/**
 * Return variable from an array
 *
 * If field $name does not exists in array this function will return $default
 *
 * @param array $from Hash
 * @param string $name
 * @param mixed $default
 * @param boolean $and_unset
 * @return mixed
 */
function array_var(& $from, $name, $default = null, $and_unset = false) {
	if (is_array($from) || (is_object($from) && instance_of($from, 'ArrayAccess'))) {
		if ($and_unset) {
			if (array_key_exists($name, $from)) {
				$result = $from[$name];
				unset ($from[$name]);
				return $result;
			} // if
		} else {
			return array_key_exists($name, $from) ? $from[$name] : $default;
		} // if
	} // if
	return $default;
} // array_var

/**
 *
 *
 */
function build_breakcum($sid, $breakCumb) {
	$result = $breakCumb;
	if (intval($sid) > 1) {
		$CI = & get_instance();
		$CI->load->model('Model_nav', 'model_nav');

		$parNode = $CI->model_nav->find($sid);
		if (!empty ($parNode)) {
			$result[] = $parNode;
			return build_breakcum($parNode['sid'], $result);
		}
	}
	$result = array_reverse($result);
	return $result;
}
function send_mail($to, $subject, $message, $from = null, $name = null) {
	$CI = & get_instance();
	$config = $CI->config->item('email');
	$CI->load->library('email');
	$CI->email->initialize($config);

	$CI->email->to($to);
	if ($from != null) {
		$CI->email->from($from, $name);
		//$CI->email->from('trangdt@purpleasia.com', 'dang thu trnag');
	}
	$CI->email->from('trangdt@purpleasia.com', 'dang thu trnag');
	$CI->email->subject($subject);
	$CI->email->message($message);

	$result = $CI->email->send();
	echo "<!--" . $CI->email->print_debugger() . "-->";
	return $result;
}


function generatePassword($length = 9, $strength = 0) {
	$vowels = 'aeuy';
	$consonants = 'bdghjmnpqrstvz';
	if ($strength & 1) {
		$consonants .= 'BDGHJLMNPQRSTVWXZ';
	}
	if ($strength & 2) {
		$vowels .= "AEUY";
	}
	if ($strength & 4) {
		$consonants .= '23456789';
	}
	if ($strength & 8) {
		$consonants .= '@#$%';
	}

	$password = '';
	$alt = time() % 2;
	for ($i = 0; $i < $length; $i++) {
		if ($alt == 1) {
			$password .= $consonants[(rand() % strlen($consonants))];
			$alt = 0;
		} else {
			$password .= $vowels[(rand() % strlen($vowels))];
			$alt = 1;
		}
	}
	return $password;
}

/**
 *
 *
 */
function buildPassword($passwd){
	return md5($passwd);
}

/**
 *
 *
 */
function parse_input($prefix, $data){
	$CI =& get_instance();
	$result = array();
	if(is_array($result)){
		foreach ($data as $key => $val) {
			$pos = strpos($key, $prefix);
			if ($pos === false) {
			}
			else{
				$result[substr($key, strlen($prefix))] = $CI->input->post($key);
			}
		}
	}
	return $result;
}
?>
